package object;

class Node{
    public String value;
    //每个元素
    public Node next;
    //每个节点

    public Node(String value) {
        this.value = value;
        this.next = null;
    }
}

public class MyLinkedList {
    private Node head = null;

    public int size(){
        int size = 0;
        for(Node cur = head ; cur != null; cur = cur.next){
            size++;
        }
        return size;
    }

    public void addFirst(String value){
        Node newNode = new Node(value);
        newNode.next = head;
        head = newNode;
    }

    public void add(int index,String value){
        if(index < 0 || index > size()){
            throw new RuntimeException("下标越界");
        }
        if(index == 0){
           addFirst(value);
           return;
        }
       Node prev = head;
        for(int i =0 ; i<index-1 ; i++){
            prev = prev.next;
        }
        Node newNode = new Node(value);
        newNode.next = prev.next;
        prev.next = newNode;
    }

    public void addLast(String value){
        if(head == null){
            Node newNode = new Node(value);
            head = newNode;
            return;
        }

        Node tail = head;
        for(;tail != null;tail = tail.next){
            if(tail.next == null){
                break;
            }
        }
        Node newNode = new Node(value);
        tail.next = newNode;
        newNode.next = null;
    }

    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for(Node cur = head;cur != null;cur = cur.next){
            stringBuilder.append(cur.value);
            if(cur.next != null){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    public boolean contain(String value){
        for(Node cur = head; cur != null; cur = cur.next){
            if(cur.value.equals(cur.value)){
                return true;
            }
        }
        return false;
    }

    public void clear(){
        head = null;
    }

    public int indexOf(String value){
        int index = 0;
        for(Node cur = head;cur != null;cur = cur.next){
            if(cur.value.equals(value)){
                return index;
            }
            index++;
        }
        return -1;
    }

    public void remove(int index){
        if(index < 0 || index >= size()){
            throw new RuntimeException("下标越界");
        }
        if(index == 0){
            head = head.next;
            return;
        }
        Node prev= head;
        for(int i = 0;i < index -1;i++){
            prev = prev.next;
        }
        Node toRemove = prev.next;
        prev.next = toRemove.next;
    }

    public void remove(String value){
        if(head == null){
            return;
        }
        if(head.value.equals(value)){
            head = head.next;
            return;
        }
        Node prev = head;
        for(;prev != null;prev = prev.next){
            if(prev.next.value.equals(value) && prev.next != null){
                break;
            }
        }
        if(prev == null){
            return;
        }
        Node toRemove = prev.next;
        prev.next = toRemove.next;
    }




    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst("1");
        myLinkedList.addFirst("2");
        myLinkedList.addFirst("3");
        myLinkedList.addFirst("4");
        myLinkedList.addFirst("5");
        myLinkedList.remove(2);
        System.out.println(myLinkedList);

    }




}
